/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ZoneQunZuSettingActivity.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-14 下午1:52:55   
 * @version V1.0     
 */
package com.jiyi.activity.zone;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.CheckUtil;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: ZoneQunZuSettingActivity
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-14 下午1:52:55
 * 
 */
public class ZoneQunZuSettingActivity extends ActivityBase {

	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.etQunZuName)
	EditText etQunZuName;
	@BindView(R.id.etQunZuJianJie)
	EditText etQunZuJianJie;

	String groupId,groupName,groupJieShao;

	// 加载进度圈
	Dialog dialog;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_qunzu_setting);
		ButterKnife.bind(this);
		init();
	};
	
	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}

	private void init() {
		getIntentData();
		setViewState();
		setTop();
	}

	/**
	 * @author: HYM
	 * @Title: getIntentData
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-7-20
	 */
	private void getIntentData() {
		groupId = getIntent().getStringExtra("groupId");
		groupName = getIntent().getStringExtra("groupName");
		groupJieShao = getIntent().getStringExtra("groupJieShao");
	}

	/**
	 * @author: HYM
	 * @Title: setViewState
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-7-17
	 */
	private void setViewState() {
		etQunZuName.setTag(R.id.tag_view_empty, "群名称不能为空");
		etQunZuJianJie.setTag(R.id.tag_view_empty, "群简介不能为空");
		
		etQunZuName.setText(groupName);
		etQunZuJianJie.setText(groupJieShao);
	}

	private void setTop() {
		tvBack.setText("");
		tvTitle.setText("群组信息");
		tvSearch.setCompoundDrawables(null, null, null, null);
		tvSearch.setText("保存");
	};

	@OnClick({ R.id.tvBack, R.id.tvSearch })
	public void onClick(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvSearch:
			if (CheckUtil.checkEditText(etQunZuName, etQunZuJianJie)) {
				// 执行保存
				doPost();
			}
			break;
		default:
			break;
		}
	}

	void doPost() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", groupId);
		params.put("group_name", etQunZuName.getText().toString());
		params.put("group_introduction", etQunZuJianJie.getText().toString());

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, WebApi.ZONE_GROUP_INFO_EDIT, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						if (this == null || isFinishing()) {
							return;
						}
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								Intent intent = new Intent();
								intent.putExtra("qunZuName", etQunZuName.getText().toString());
								intent.putExtra("jianJie", etQunZuJianJie.getText().toString());
								setResult(RESULT_OK, intent);
								finish();
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
						hideDialog(dialog);
					}
				});

	}
}
